package tr.edu.yildiz.basevaadin.service.impl;

import tr.edu.yildiz.basevaadin.service.*;
import tr.edu.yildiz.dynaView.service.Module;
import tr.edu.yildiz.dynaView.service.ModuleService;
import tr.edu.yildiz.dynaView.service.ModuleServiceListener;

import java.util.*;

import com.vaadin.ui.Component;

public class ModuleServiceImpl implements ModuleService<Component> {

	private ArrayList<Module<Component>> modules = new ArrayList<Module<Component>>();
	
	private ArrayList<ModuleServiceListener<Component>> listeners = new ArrayList<ModuleServiceListener<Component>>();

	@SuppressWarnings("unchecked")
	public synchronized void registerModule(Module module) {
		System.out.println("ModuleServiceImpl: Registering module " + module);
		modules.add(module);
		for (ModuleServiceListener listener : (ArrayList<ModuleServiceListener>) listeners.clone()) {
			listener.moduleRegistered(this, module);
		}
	}
	
	@SuppressWarnings("unchecked")
	public synchronized void unregisterModule(Module module) {
		System.out.println("ModuleServiceImpl: Unregistering module " + module);
		modules.remove(module);
		for (ModuleServiceListener listener : (ArrayList<ModuleServiceListener>) listeners.clone()) {
			listener.moduleUnregistered(this, module);
		}
	}
	
	public List<Module<Component>> getModules() {
		return Collections.unmodifiableList(modules);
	}
	
	public synchronized void addListener(ModuleServiceListener listener) {
		System.out.println("ModuleServiceImpl: Adding listener " + listener);
		listeners.add(listener);
	}
	
	public synchronized void removeListener(ModuleServiceListener listener) {
		System.out.println("ModuleServiceImpl: Removing listener " + listener);
		listeners.remove(listener);
	}


}